package com.itheima.tlias.controller;

import com.itheima.tlias.annotation.Log;
import com.itheima.tlias.pojo.vo.PageBean;
import com.itheima.tlias.common.result.Result;
import com.itheima.tlias.pojo.dto.SelectempDetailDTO;
import com.itheima.tlias.pojo.po.InsertEmpParam;
import com.itheima.tlias.pojo.po.SelectEmpPageParam;
import com.itheima.tlias.pojo.po.UpdateEmpParam;
import com.itheima.tlias.service.EmpService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/emps")
public class EmpController {
    //注入他的接口，利用多态进行赋值
    @Resource
    private EmpService empService;

    @GetMapping
    public Result selectEmpAll(SelectEmpPageParam param) {
        log.info("查询参数{}",param);
        PageBean pageBean = empService.selectEmpByPageParam(param);

        return Result.success(pageBean);
    }

    /**
     * @Validated 启用校验
     * @param param
     * @return
     */
    @Log
    @PostMapping
    public Result insertEmp(@Validated @RequestBody InsertEmpParam param) {
        log.info("新增员工参数{}",param);
        int i = empService.insertEmp(param);
        return i ==0?  Result.error("新增员工失败"): Result.success();
    }
    @Log
    @DeleteMapping
    public Result batchDeleteEmpByIds(@RequestParam("ids") List<Integer> ids){
        log.info("删除员工,{}",ids);
        if (ids.isEmpty()){
            return Result.error("请先选择id");
        }
        int flag = empService.batchDeleteEmpByIds(ids);
        return flag == 0? Result.error("删除失败"): Result.success();
    }
    @GetMapping("/{id}")
    public Result selectEmpById(@PathVariable("id")Integer id){
        log.info("查询员工id{}",id);
        SelectempDetailDTO emp =empService.selectEmpById(id);
        return Result.success(emp);

    }
    @Log
    @PutMapping
    public Result updateEmp(@RequestBody UpdateEmpParam param){
        log.info("修改员工信息{}",param);
        int flag = empService.updateEmp(param);
        return flag == 0? Result.error("修改失败"): Result.success();
    }

    @GetMapping("/list")
    public Result selectEmpAll(){
        log.info("查询所有员工信息");
        List<SelectempDetailDTO> dto  = empService.selectEmpAll();
        return Result.success(dto);
    }

}
